<?php

/**
 * article actions.
 *
 * @package    myarticles
 * @subpackage article
 * @author     Apul Gupta<apulmca2k4@gmail.com>
 */
class articleActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $this->articles = ArticlePeer::doSelect(ArticlePeer::getCurrentUserCriteria($this->getUser()->getAttribute('user_id')));
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new ArticleForm(array(), array('isFront' => true));
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new ArticleForm(array(), array('isFront' => true));

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($article = ArticlePeer::retrieveByPk($request->getParameter('id')), sprintf('Object article does not exist (%s).', $request->getParameter('id')));
        $this->forward404Unless($article->getUserId() == $this->getUser()->getAttribute('user_id'));
        $this->form = new ArticleForm($article, array('isFront' => true));
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($article = ArticlePeer::retrieveByPk($request->getParameter('id')), sprintf('Object article does not exist (%s).', $request->getParameter('id')));
        $this->form = new ArticleForm($article, array('isFront' => true));

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($article = ArticlePeer::retrieveByPk($request->getParameter('id')), sprintf('Object article does not exist (%s).', $request->getParameter('id')));
        $article->delete();

        $this->redirect('article/index');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            // check for current user
            // If User logged in
            if ($this->getUser()->hasAttribute('user_id')) {
                $form->setValue('user_id', $this->getUser()->getAttribute('user_id'));
                $article = $form->save();
                $this->redirect('user/home');
            } else  {// Not logged in
                // redirect to author sign-up/sign-in page
                $this->getUser()->setAttribute('form', $form);
                $this->redirect('user/sign');
            }

            $this->redirect('article/edit?id=' . $article->getId());
        }
    }

    public function executeSearch(sfWebRequest $request) {
        $title = $request->getParameter('keyword');
        $this->keyword = $title;
        $this->limit = sfConfig::get('app_article_search_result');
        $this->pager = new sfPropelPager('Article', $this->limit);
        $this->pager->setCriteria(ArticlePeer::getSearchCriteria($title));
        $this->pager->setPage($request->getParameter('page', 1));
        $this->pager->init();
    }

    public function executeShow(sfWebRequest $request) {
        $this->article = $this->getRoute()->getObject();
        $this->forward404Unless($this->article);

        $this->article->setArticleViews($this->article->getArticleViews() + 1);
        $this->article->save();


        if ($this->getUser()->hasAttribute('form')) {
            $this->form = $this->getUser()->getAttribute('form');
        } else {
            $this->form = new CommentForm();
            $this->form->setDefault('article_id', $this->article->getId());
        }

        $this->getResponse()->setTitle(html_entity_decode($this->article->getMetaTitle()), false);
        $this->getResponse()->addMeta('description', html_entity_decode($this->article->getMetaDescription()));
        $this->getResponse()->addMeta('keywords', html_entity_decode($this->article->getMetaKeyword()));
    }

    public function executePreview(sfWebRequest $request) {
        // Fetch Article from Database
        $this->article = $this->getRoute()->getObject();
        $this->forward404Unless($this->article);

        $this->setTemplate('show');
    }

}